using System;
using System.Collections.Generic;
using System.Diagnostics;
using FishFillets.Model;
using System.Resources;
using System.Reflection;
using System.Globalization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace FishFillets.ObjectLogic
{
    public class DialogManager
    {
        public Queue<Dialog> Dialogs { get; private set; }
        public Dialog CurrentDialog { get; set; }
        protected ContentManager contentManager;

        public StringsCollection LoadedLines { get; set; }

        public DialogManager()
        {
            this.Dialogs = new Queue<Dialog>();
        }

        public void LoadStrings(StringsCollection lines) {
            this.LoadedLines = lines;
        }
        public void UnloadLines()
        {
            this.LoadedLines = null;
            this.CurrentDialog = null;
        }
        
        /// <summary>
        /// Adds a dialog line for Item.
        /// </summary>
        /// <param name="delay">Delay before displaying the dialog line after the queued dialog before this one, in tenths of a milisecond (0,X s)</param>
        /// <param name="messageID">The message line ID to display.</param>
        /// <param name="color">Color related to the target item.</param>
        public void AddItem(long delay, string messageID, Color color)
        {
            AddMessage(TimeSpan.FromMilliseconds(delay * 100), messageID, color);
        }

        public void AddDelay(long delay)
        {
            Dialog dialog = new Dialog
            {
                Empty = true,
                Delay = TimeSpan.FromMilliseconds(delay * 100),
            };
            Dialogs.Enqueue(dialog);
        }

        /// <summary>
        /// Adds a dialog line for the small fish.
        /// </summary>
        /// <param name="delay">Delay before displaying the dialog line after the queued dialog before this one.</param>
        /// <param name="messageID">The message line ID to display.</param>
        /// <param name="color"></param>
        public void AddMessage(TimeSpan delay, string messageID, Color color)
        {
            Debug.Assert(!string.IsNullOrEmpty(messageID));

            Dialog dialog = new Dialog
            {
                SoundName = messageID,
                Message = GetDialogLine(messageID, "cs"),
                Delay = delay,
                Force = false,
                Color = color,
            };
            Debug.WriteLine(dialog.Message);
            Dialogs.Enqueue(dialog);
        }

        private string GetDialogLine(string messageID, string culture)
        {
            try
            {
                return LoadedLines.GetString(messageID, culture);
            }
            catch
            {
                return messageID;
            }
        }
    }
}
